<?php

namespace app\h7_tool\controller\web\recharge;

use app\wc_card\facade\service\RechargeServiceFacade;
use xsframe\base\AdminBaseController;

class Index extends AdminBaseController
{
    protected $tableName = 'wc_card_recharge';

    public function main()
    {
        $condition = [
            'uniacid' => $this->uniacid,
            'deleted' => 0,
        ];

        $list  = RechargeServiceFacade::getList($condition, "*", "money asc");
        $total = RechargeServiceFacade::getTotal($condition);
        $pager = pagination2($total, $this->pIndex, $this->pSize);

        $result = [
            'list'  => $list,
            'pager' => $pager,
            'total' => $total,
        ];
        return $this->template('web/recharge/index', $result);
    }

    public function add()
    {
        return $this->post();
    }

    public function edit()
    {
        return $this->post();
    }

    public function post()
    {
        if ($this->request->isPost()) {

            if (!empty($this->params['cate'])) {
                foreach ($this->params['cate'] as $id => $cate) {
                    $data = array(
                        'name'    => trim($cate['name']),
                        'desc'    => trim($cate['desc']),
                        'money'   => trim($cate['money']),
                        'days'    => trim($cate['days']),
                        'enabled' => trim($cate['enabled']),
                    );
                    if (!empty($id) && !empty($data['money'])) {
                        RechargeServiceFacade::updateInfo($data, ["id" => $id]);
                    }
                }
            }

            if (!empty($this->params['money_new'])) {
                $name_new    = $this->params['name_new'];
                $desc_new    = $this->params['desc_new'];
                $money_new   = $this->params['money_new'];
                $days_new    = $this->params['days_new'];
                $enabled_new = $this->params['enabled_new'];

                foreach ($money_new as $key => $cate_new) {
                    $cate_new = trim($cate_new);

                    if (empty($cate_new)) {
                        continue;
                    }

                    $data = array(
                        'uniacid'    => $this->uniacid,
                        'name'       => $name_new[$key],
                        'desc'       => $desc_new[$key],
                        'money'      => $money_new,
                        'days'       => $days_new,
                        'enabled'    => $enabled_new[$key],
                        'createtime' => time(),
                    );
                    RechargeServiceFacade::insertInfo($data);
                }
            }

            $this->success(array("url" => webUrl("web.recharge.index/main")));
        }

    }

}
